package com.wulwu.rocketmq.handler;

import com.alibaba.fastjson.JSON;
import com.wulwu.rocketmq.config.ConsumerConfig;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @Description 监听者
 * @Author wuxb
 * @Date 2022/3/9 18:09
 */
@Component
public class RocketMsgListener implements MessageListenerConcurrently {

    private static final Logger LOGGER = LoggerFactory.getLogger(RocketMsgListener.class);

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
        MessageExt messageExt = msgs.get(0);
        try {
            String body = new String(messageExt.getBody(), "utf-8");
            LOGGER.info("consumeMessage:{}", JSON.toJSONString(body));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }
}
